#include "wave_generate.h"

#define Sin_Path "0:_wave/_default/sin.wda"
#define Tri_Path "0:_wave/_default/tri.wda"
#define Saw_Path "0:_wave/_default/saw.wda"
#define Sqa_Path "0:_wave/_default/sqa.wda"

#define PI 3.1415926


extern FIL waveFile;
extern FATFS fs;
extern DIR dirs;
extern unsigned int br;
extern char fatfr;

void waveGenerate_sin(unsigned int count , unsigned int width)
{
	double outNum;	//64bits
	double stepLen;
	unsigned int resNum;
	unsigned long i;
	
	//USB_Interrupts_Disable();
	
	printf("\r\n\nGenerate Sin Wave Data ... ");
	f_mount(0,&fs);
	if((fatfr = f_open(&waveFile, Sin_Path , FA_CREATE_ALWAYS | FA_WRITE)) == FR_OK)
	{
		printf("\r\nCreat Wave File ... OK\r\nStart Writing ...");
	}
	else
	{
		printf("\r\nCreat File Error : %d",fatfr);
		return;
	}
	
	/*Writing config information*/
	f_write(&waveFile,&width,4,&br);
	f_write(&waveFile,&count,4,&br);
	
	
	stepLen = (2 * PI) / (double)count;
	//printf("\r\nStep Length : %lf",stepLen);
	
	for(i=0 ; i<count ; i++)
	{
		outNum = sin(i * stepLen);
		resNum = (unsigned int)(((outNum + 1)/2) * (2 << (width-1)));
		if(resNum >=  (2 << (width-1)))
			resNum = (2 << (width-1)) -1;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	
	printf("\r\nOK!!!\r\n");
	f_close(&waveFile);
}

void waveGenerate_tri(unsigned int count , unsigned int width)
{
	double outNum;	//64bits
	double stepLen;
	unsigned int resNum;
	unsigned long i;
	
	//USB_Interrupts_Disable();
	
	printf("\r\n\nGenerate Tri Wave Data ... ");
	f_mount(0,&fs);
	if((fatfr = f_open(&waveFile, Tri_Path , FA_CREATE_ALWAYS | FA_WRITE)) == FR_OK)
	{
		printf("\r\nCreat Wave File ... OK\r\nStart Writing ...");
	}
	else
	{
		printf("\r\nCreat File Error : %d",fatfr);
		return;
	}
	
	/*Writing config information*/
	f_write(&waveFile,&width,4,&br);
	f_write(&waveFile,&count,4,&br);
	
	
	stepLen = (2 << (width-1)) / (double)(count >> 1);
	//printf("\r\nStep Length : %lf",stepLen);
	
	for(i=0 ; i<(count>>1) ; i++)
	{
		outNum = i * stepLen;
		resNum = (unsigned int)outNum;
		if(resNum >=  (2 << (width-1)))
			resNum = (2 << (width-1)) -1;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	for(; i<count ; i++)
	{
		outNum = (count - i) * stepLen;
		resNum = (unsigned int)outNum;
		if(resNum >=  (2 << (width-1)))
			resNum = (2 << (width-1)) -1;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	
	printf("\r\nOK!!!\r\n");
	f_close(&waveFile);
}

void waveGenerate_saw(unsigned int count , unsigned int width)
{
	double outNum;	//64bits
	double stepLen;
	unsigned int resNum;
	unsigned long i;
	
	//USB_Interrupts_Disable();
	
	printf("\r\n\nGenerate Saw Wave Data ... ");
	f_mount(0,&fs);
	if((fatfr = f_open(&waveFile, Saw_Path , FA_CREATE_ALWAYS | FA_WRITE)) == FR_OK)
	{
		printf("\r\nCreat Wave File ... OK\r\nStart Writing ...");
	}
	else
	{
		printf("\r\nCreat File Error : %d",fatfr);
		return;
	}
	
	/*Writing config information*/
	f_write(&waveFile,&width,4,&br);
	f_write(&waveFile,&count,4,&br);
	
	
	stepLen = (2 << (width-1)) / (double)count;
	//printf("\r\nStep Length : %lf",stepLen);
	
	for(i=0 ; i<count ; i++)
	{
		outNum = i * stepLen;
		resNum = (unsigned int)outNum;
		if(resNum >=  (2 << (width-1)))
			resNum = (2 << (width-1)) -1;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	
	printf("\r\nOK!!!\r\n");
	f_close(&waveFile);
}

void waveGenerate_sqa(unsigned int count , unsigned int width)
{
	double outNum;	//64bits
	//double stepLen;
	unsigned int resNum;
	unsigned long i;
	
	//USB_Interrupts_Disable();
	
	printf("\r\n\nGenerate Sqa Wave Data ... ");
	f_mount(0,&fs);
	if((fatfr = f_open(&waveFile, Sqa_Path , FA_CREATE_ALWAYS | FA_WRITE)) == FR_OK)
	{
		printf("\r\nCreat Wave File ... OK\r\nStart Writing ...");
	}
	else
	{
		printf("\r\nCreat File Error : %d",fatfr);
		return;
	}
	
	/*Writing config information*/
	f_write(&waveFile,&width,4,&br);
	f_write(&waveFile,&count,4,&br);
	
	
	//stepLen = (2 << (width-1)) / (double)(count >> 1);
	//printf("\r\nStep Length : %lf",stepLen);
	
	for(i=0 ; i<(count>>1) ; i++)
	{
		outNum = 0;
		resNum = (unsigned int)outNum;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	for(; i<count ; i++)
	{
		outNum = (2 << (width-1)) -1;
		resNum = (unsigned int)outNum;
		//printf("\r\n %ld",resNum);
		f_write(&waveFile,&resNum,4,&br);
	}
	
	printf("\r\nOK!!!\r\n");
	f_close(&waveFile);
}

